package com.bdsoft.actions.resourceManage.Controller;

import com.bdsoft.actions.AcUrls;
import com.bdsoft.actions.resourceManage.DTO.ResourceDTO;
import com.bdsoft.actions.resourceManage.Service.ResourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping(AcUrls.ResourceManage.resourceManageUrl)
@Tag(name = "资源相关接口")
public class ResourceController {

    @Autowired
    ResourceService resourceService;

    @Operation(summary = "获取资源分页列表")
    @PostMapping(AcUrls.ResourceManage.getResourceList)
    public ResponseEntity getResourceList(@RequestParam int pageNum, @RequestParam int pageSize,
                                          @RequestParam(required = false) String resourceId,
                                          @RequestParam(required = false) String resourceName) {
        return ResponseEntity.ok(resourceService.getResourceList(pageNum, pageSize, resourceId, resourceName));
    }

    @Operation(summary = "根据角色id获取已分配资源")
    @PostMapping(AcUrls.ResourceManage.getResourceListByRole)
    public ResponseEntity getResourceListByRole(String roleId) {
        return ResponseEntity.ok(resourceService.getResourceListByRole(roleId));
    }

    @Operation(summary = "保存/修改资源")
    @PostMapping(AcUrls.ResourceManage.saveOrUpdateResource)
    public Map<String,Object> saveOrUpdateResource(@RequestBody ResourceDTO resourceDTO) {
        return resourceService.saveOrUpdateResource(resourceDTO);
    }

    @Operation(summary = "删除资源")
    @PostMapping(AcUrls.ResourceManage.deleteResource)
    public Map<String,Object> deleteResourceById(@RequestParam String resourceId) {
        return resourceService.deleteResourceById(resourceId);
    }

    @Operation(summary = "为角色分配资源")
    @PostMapping(AcUrls.ResourceManage.assignResourceToRole)
    public Map<String,Object> assignResourceToRole(@RequestParam String roleId, @RequestParam String resourceId) {
        return resourceService.assignResourceToRole(roleId, resourceId);
    }

    @Operation(summary = "移除角色的资源")
    @PostMapping(AcUrls.ResourceManage.removeResourceFromRole)
    public Map<String,Object> removeResourceFromRole(@RequestParam String roleId, @RequestParam String resourceId) {
        return resourceService.removeResourceFromRole(roleId, resourceId);
    }
} 